<?php

require_once ('core_model.php');
/**
 * 分类
 * @author kisskizz
 * @version 1.0
 * @created 09-六月-2008 20:35:32
 */
class Catagory_model extends Core_model {
	protected $_table = "thankphp_catagory";
	/* Pure Object */
	public $catagory_create = array(
		'action'=>'catagory/create',
		'base'=>'catagory',
		'type'=>'data',
		'title'=>'Catagory Create',
		'act'=>'create',
		'fields'=>array(
			array(
				'name'=>'catagory-name','id'=>'catagory-name','maxlength'=>20,'value'=>'Please Input the Catagory Name','type'=>'text'
			)	
		)
	);
	public function __construct() {
		parent::__construct();
		$this->load->model("display");
	}

	public function __destruct() {

	}

	public function form_create() {
		return $this->catagory_create;
	}

	public function show() {
		$data["catagory"] = array("catagorys"=>$this->retriveby());
		$data["catagory_form"] = array("form"=>form_generate($this->catagory_create));
		$this->display->mainpage($data);
		unset($data);
	}
	
	public function create($data) {
		$result = parent::create($data);
		unset($data);
		if(is_int($result)) {
			$data['information'] = array("information"=>"目录添加成功");
			$this->display->mainpage($data);
			unset($data);
		}
	}
	/**
	 * 
	 * @param catagory
	 */
	public function getIdByCatagory($catagory = '') {
		$this->db->select("id");
		$this->db->where("catagory_alias",$catagory);
		$query = $this->db->get($this->_table);
		if($query->num_rows() >0 ) {
			$result = $query->row_array();
			return $result['id'];
		} else {
			return false;
		}
	}

	//查询分类下的书 数量
	public function getNumById($id) {
		$sql = sprintf("select count(1) from `%s` where `catagory_id` = %d","qj_thankphp_ebook",$id);
		echo $sql;
		exit;
		$q = $this->db->query($sql);
		if($q->num_rows()>0) {
			return $q->num_rows();
		}
	}

	//得到分类列表
	function getMenus() {
		return $this->retriveby();
	}
	
	/**
	 * 后台管理
	 */
	function admin() {
		$data = array("catagory"=>array("catagorys"=>$this->getMenus()));
		return $data;
	}
}
?>